package core;

import gitobject.*;
import repository.Repository;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class JitAdd {
	/**
     * Add files into your index.
     * @param filename
     * @throws IOException 
     */
    @SuppressWarnings("unused")
	public static void add(String filename) throws IOException {
        /* Todo: You should pass the filename in this function, and generate a hash file in your repository.
        *   Add your code here.*/
    	Date date = new Date();
    	try {
			String path = Repository.getWorkTree() + File.separator + filename;
			String fileKey = filename.replace(File.separator, Tree.treeSeparator);
			File file = new File(path);
			if(file.exists()) {
				if (file.isDirectory())
					addBlob(filename);
				else if (file.isFile())
					createBlob(file, fileKey, filename);
			}
	    	else System.out.printf("fatal: pathspec '%s' did not match any files\n", filename);
			JitMap.index.compressWrite();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void createBlob(File file, String keyPath, String path) throws Exception {
		Date date = new Date();
		Blob blob = new Blob(file);
		boolean inCommitMap = JitMap.commitMap.containsKey(keyPath);
		boolean inIndex = JitMap.index.contains(keyPath);
		String indexContent = blob.toString() + " " + date.toString() + " " + keyPath;
		String printContent = blob.toString() + " " + date.toString() + " " + path;
		if(!inCommitMap || !Objects.equals(JitMap.commitMap.get(keyPath), blob.getKey())){
			if(!inIndex || !Objects.equals(JitMap.index.getIndexKey(keyPath), blob.getKey())){
				if(blob.writeBlob()) {
					JitMap.index.putIndex(keyPath, indexContent);
					JitMap.changeStatusInIndex(keyPath);
					System.out.println(printContent);
				}
				else{
					System.out.printf("\"%s\" adds failed\r\n",path);
				}
			}
		}
	}

    private static void addBlob(String path) throws Exception{
    	File dir = new File(Repository.getWorkTree() + File.separator + path);
		String[] fs = dir.list();
		assert fs != null;
		for(String file: fs) {
			String newPath = Objects.equals(path, ".") ? file : path + File.separator + file;
			String keyPath = newPath.replace(File.separator, Tree.treeSeparator);
			File item = new File(Repository.getWorkTree() + File.separator + newPath);
			if(item.isFile()) {
				createBlob(item, keyPath, newPath);
			}
			else if(item.isDirectory()) {
				if(Objects.equals(file, ".jit")) continue;
				JitMap.changeStatusInCommit(keyPath);
				addBlob(newPath);
			}
		}
    }

    public static void checkDelete(){

	}

    //用法存在问题
    public static String fileState(String path) throws Exception{
    	try {
	    	//System.out.println(Repository.getWorkTree() + File.separator + path);
			String keyPath = path.replace(File.separator, Tree.treeSeparator);
			File item = new File(Repository.getWorkTree() + File.separator + path);
			Blob blob = new Blob(item);
			if(!JitMap.commitMap.containsKey(keyPath) && !Objects.equals(JitMap.commitMap.get(keyPath), blob.getKey()) && !(JitMap.index.contains(keyPath))) {
				return "changed";
			}
			else if(JitMap.index.contains(keyPath)){
				return "added";
			}
			else {
				return "";
			}
	    } catch (IOException e1) {
			e1.printStackTrace();
		}
    	return "changed";
    }

}